// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using InterpriseSuiteEcommerceCommon;
using InterpriseSuiteEcommerceCommon.DTO;
using InterpriseSuiteEcommerceCommon.InterpriseIntegration.JSONLib;

namespace InterpriseSuiteEcommerceCommon.Actions
{
    public class OrderHistoryAction : IActionHandler
    {
        #region IActionHandler Members

        public string Name
        {
            get { return "orderHistory"; }
        }

        public void Handle(System.Web.HttpContext context)
        {
            Customer thisCustomer = Customer.Current;
            if(thisCustomer.IsRegistered)
            {
                int pages   = CommonLogic.FormUSInt("Pages");
                int current = CommonLogic.FormUSInt("Current");
                SalesOrderHistoryCollection allOrders = SalesOrderHistoryCollection.ForCustomer(thisCustomer, pages, current);
                OrderHistoryResult result = new OrderHistoryResult();
                result.current = allOrders.Current;
                result.all = allOrders.AllPages;
                result.html = allOrders.Display();

                JSONSerializer ser = new JSONSerializer(SerializeOption.Fields);
                string output = ser.Serialize(result);

                context.Response.Clear();
                context.Response.Write(output);
                context.Response.Flush();
                context.Response.End();
            }
        }

        #endregion
    }

    public class OrderHistoryResult
    {
        public string html = string.Empty;
        public int current = 0;
        public int all = 0;
    }
}
